package {
	import navigation.TreeNavigation;

	import pages.LoginPage;
	import pages.TestPage;

	import util.TextFieldFactory;

	import org.splink.deepsplink.boot.ExternalConfigBootstrapper;
	import org.splink.deepsplink.boot.IBootStrategy;
	import org.splink.deepsplink.boot.IDeepsplinkFacade;
	import org.splink.library.logging.ILogger;
	import org.splink.library.logging.ILoggerFactory;
	import org.splink.library.logging.LogLevel;
	import org.splink.library.logging.LogRange;
	import org.splink.library.logging.LoggerFactory;
	import org.splink.library.logging.LoggerProvider;
	import org.splink.library.logging.logoutput.QLogOutput;
	import org.splink.library.logging.logoutput.TraceOutput;
	import org.splink.library.queue.IQ;
	import org.splink.library.queue.Q;
	import org.splink.library.queue.QEvent;
	import org.splink.resource.ExternalResourceProcessor;
	import org.splink.resource.IResourceProcessor;
	import org.splink.resource.ResourceProcessorEvent;
	import org.splink.resource.ResourceProgress;

	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.text.TextField;

	/**
	 * @author Max Kugland
	 */
	public class SampleApplication extends Sprite implements IBootStrategy {
		private var _logger : ILogger;
		private var _progress : TextField;

		private function configureLogger() : void {
			var factory : ILoggerFactory = new LoggerFactory("max");
			factory.range = new LogRange(LogLevel.TRACE, LogLevel.FATAL);
			factory.addLogOutput(new QLogOutput());
			factory.addLogOutput(new TraceOutput());
			LoggerProvider.addLoggerFactory(factory);
			_logger = LoggerProvider.getLogger("max", SampleApplication);
		}


		public function SampleApplication() {
			configureLogger();
			// Classes are only defined within config.xml and not included in the swf unless they are referenced.
			TestPage;
			LoginPage;
			
			scrollRect = new Rectangle(0, 0, 550, 400);
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.stageFocusRect = false;
			stage.addEventListener(Event.RESIZE, center);
			
			initialize();
			center();
		}

		private function center(e : Event = null) : void {
			x = stage.stageWidth / 2 - width / 2;			y = stage.stageHeight / 2 - height / 2;
		}

		private function initialize() : void {
			_logger.log(LogLevel.INFO, "init");
			addChild(_progress = TextFieldFactory.singleline("...", 12));
			
			var q : IQ = new Q();
			q.register(QEvent.COMPLETE, function(e : QEvent) : void {
				removeChild(_progress);
			});
			q.register(QEvent.ERROR, function(e : QEvent) : void {
				_logger.log(LogLevel.ERROR, e.errorMessage);
			});
			
			var resourceProcessor : IResourceProcessor = new ExternalResourceProcessor("config/resourcebundles.xml");
			resourceProcessor.register(ResourceProcessorEvent.PROGRESS, onProgress);
			
			q.add(resourceProcessor);
			q.add(new ExternalConfigBootstrapper("config/config.xml", this));
			q.start();
		}
		
		/**
		 * @inheritDoc
		 */
		public function boot(facade : IDeepsplinkFacade) : void {
			addChild(facade.display);
			
			var treeNavi : TreeNavigation = new TreeNavigation(facade.config.pageData.toXML(), facade.navigationCommand, ["404"]);
			facade.navigationCommand.registerNavigation(treeNavi);
			addChild(treeNavi.display).y = 10;

			facade.registerAuthenticator(new MockAuthenticator());
			
			facade.display.x = 150;
		}

		private function onProgress(e : ResourceProcessorEvent) : void {
			var progress : ResourceProgress = e.resourceProgress;
			_progress.htmlText = "Loading: " + progress.currentId + " " + progress.totalPercent + "%";
		}
	}
}